package ui;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashSet;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import util.TimeUtil;

import data.Constant;
import data.LoadData;
import data.QuerySolutionInfo;
import data.TimeStock;
import dataconnect.ConnectionFactory;
import dataconnect.SqlUtil;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

public class ManualSelectUI {

	protected Shell shell;
	private Text txtCurPerUp;
	private Text txtCurPerDown;
	private Text txtDaysDown;
	private Text txtDownPer;
	private Text txtUpPer;
	private Text txtDaysUp;
	private Text txtLowBack;
	private Text txtDwonDays;
	private Text txtUpDays;
	private Text txtScoreHigher;
	private Text txtScoreLower;
	private Text txtIndustry;
	private Text txtDaysDeal;
	private Text txtDealBat;
	private Text txtWaveLarger;
	private Text txtIdeal;
	private Text txtMaxToDays;
	private Text txtMinToDays;
	private Text txtWaveLower;

	private Button btnDaysUp;
	private Button btnDaysdown;

	private LinkedHashSet<String> codeSet = new LinkedHashSet<String>();

	private StringBuffer where = new StringBuffer(" where 1 = 1 ");
	private Table table;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			ManualSelectUI window = new ManualSelectUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public LinkedHashSet<String> open() {
		Display display = Display.getDefault();
		createContents();
		shell.setLocation(Display.getCurrent().getClientArea().width / 2
				- shell.getShell().getSize().x / 2, Display.getCurrent()
				.getClientArea().height / 2 - shell.getSize().y / 2);
		
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		return codeSet;
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(864, 484);
		shell.setText("\u624B\u5DE5\u9009\u80A1");

		final Button btnCurPerUp = new Button(shell, SWT.CHECK);
		btnCurPerUp.setText("\u5F53\u5929\u6DA8\u5E45\u5927\u4E8E");
		btnCurPerUp.setBounds(294, 29, 93, 21);

		final Button btnCurPerDown = new Button(shell, SWT.CHECK);
		btnCurPerDown.setText("\u5F53\u5929\u8DCC\u5E45\u5C0F\u4E8E");
		btnCurPerDown.setBounds(294, 58, 93, 21);

		final Button btnCurOverMaxPrice = new Button(shell, SWT.CHECK);
		btnCurOverMaxPrice
				.setText("\u5F53\u524D\u8D85\u8D8A\u6700\u9AD8\u4EF7");
		btnCurOverMaxPrice.setBounds(56, 31, 105, 16);

		final Button btnHighLevel = new Button(shell, SWT.CHECK);
		btnHighLevel.setBounds(56, 116, 61, 16);
		btnHighLevel.setText("\u9AD8\u4EF7\u4F4D");

		final Button btnLowLevel = new Button(shell, SWT.CHECK);
		btnLowLevel.setText("\u4F4E\u4EF7\u4F4D");
		btnLowLevel.setBounds(56, 138, 61, 16);

		txtCurPerUp = new Text(shell, SWT.BORDER);
		txtCurPerUp.setBounds(393, 27, 30, 21);

		txtCurPerDown = new Text(shell, SWT.BORDER);
		txtCurPerDown.setText("");
		txtCurPerDown.setBounds(393, 56, 30, 21);

		btnDaysdown = new Button(shell, SWT.CHECK);
		btnDaysdown.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btnDaysUp.setSelection(false);
			}
		});
		btnDaysdown.setBounds(56, 254, 30, 21);
		btnDaysdown.setText("\u8FD1");

		txtDaysDown = new Text(shell, SWT.BORDER);
		txtDaysDown.setText("2");
		txtDaysDown.setBounds(92, 252, 30, 21);

		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(124, 256, 61, 16);
		lblNewLabel.setText("\u5929\u8DCC\u5E45\u8D85\u8FC7");

		txtDownPer = new Text(shell, SWT.BORDER);
		txtDownPer.setText("-10");
		txtDownPer.setBounds(184, 252, 30, 21);

		txtUpPer = new Text(shell, SWT.BORDER);
		txtUpPer.setText("10");
		txtUpPer.setBounds(184, 276, 30, 21);

		Label label = new Label(shell, SWT.NONE);
		label.setText("\u5929\u6DA8\u5E45\u8D85\u8FC7");
		label.setBounds(124, 280, 61, 16);

		txtDaysUp = new Text(shell, SWT.BORDER);
		txtDaysUp.setText("2");
		txtDaysUp.setBounds(92, 276, 30, 21);

		btnDaysUp = new Button(shell, SWT.CHECK);
		btnDaysUp.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				btnDaysdown.setSelection(false);
			}
		});
		btnDaysUp.setText("\u8FD1");
		btnDaysUp.setBounds(56, 278, 30, 21);

		final Button btnCurUpToLow = new Button(shell, SWT.CHECK);
		btnCurUpToLow
				.setText("\u5F53\u524D\u4EF7\u9AD8\u4E8E\u6700\u4F4E\u4EF7");
		btnCurUpToLow.setBounds(294, 87, 116, 21);

		txtLowBack = new Text(shell, SWT.BORDER);
		txtLowBack.setText("2.8");
		txtLowBack.setBounds(408, 87, 30, 21);

		final Button btnCurMaxPrice = new Button(shell, SWT.CHECK);
		btnCurMaxPrice.setSelection(true);
		btnCurMaxPrice.setText("\u5F53\u5929\u6700\u9AD8\u4EF7");
		btnCurMaxPrice.setBounds(56, 53, 105, 16);

		final Button BtnLowDays = new Button(shell, SWT.CHECK);
		BtnLowDays.setText("\u8FDE\u8DCC");
		BtnLowDays.setBounds(294, 116, 45, 21);

		txtDwonDays = new Text(shell, SWT.BORDER);
		txtDwonDays.setText("2");
		txtDwonDays.setBounds(338, 114, 30, 21);

		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setBounds(374, 119, 93, 16);
		label_1.setText("\u5929\uFF0C\u4E0D\u80FD\u5927\u4E8E20");

		final Button BtnUpDays = new Button(shell, SWT.CHECK);
		BtnUpDays.setText("\u8FDE\u6DA8");
		BtnUpDays.setBounds(294, 145, 45, 21);

		txtUpDays = new Text(shell, SWT.BORDER);
		txtUpDays.setText("2");
		txtUpDays.setBounds(338, 143, 30, 21);

		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setText("\u5929\uFF0C\u4E0D\u80FD\u5927\u4E8E20");
		label_2.setBounds(374, 148, 93, 16);

		final Button btnCurBigDeal = new Button(shell, SWT.CHECK);
		btnCurBigDeal.setText("\u5F53\u5929\u91CF\u589E");
		btnCurBigDeal.setBounds(56, 75, 93, 16);
		
		final Button btnCurPreBat = new Button(shell, SWT.CHECK);
		btnCurPreBat.setText("\u5F53\u5929\u91CF\u589E(\u4E0A\u4E2A\u4EA4\u6613\u65E5)");
		btnCurPreBat.setBounds(56, 97, 137, 16);

		final Button btnScoreHigher = new Button(shell, SWT.CHECK);
		btnScoreHigher
				.setText("\u51C0\u5229\u6DA6\u589E\u957F\u7387\u5927\u4E8E");
		btnScoreHigher.setBounds(294, 174, 116, 21);

		txtScoreHigher = new Text(shell, SWT.BORDER);
		txtScoreHigher.setBounds(408, 170, 30, 21);

		final Button btnScoreLower = new Button(shell, SWT.CHECK);
		btnScoreLower
				.setText("\u51C0\u5229\u6DA6\u589E\u957F\u7387\u5C0F\u4E8E");
		btnScoreLower.setBounds(294, 203, 116, 21);

		txtScoreLower = new Text(shell, SWT.BORDER);
		txtScoreLower.setBounds(408, 203, 30, 21);

		final Button BtnIndustry = new Button(shell, SWT.CHECK);
		BtnIndustry.setText("\u884C\u4E1A\u7C7B\u4F3C");
		BtnIndustry.setBounds(294, 232, 69, 21);

		txtIndustry = new Text(shell, SWT.BORDER);
		txtIndustry.setText("");
		txtIndustry.setBounds(363, 230, 80, 21);

		final Button btnDeal = new Button(shell, SWT.CHECK);
		btnDeal.setText("\u8FD1");
		btnDeal.setBounds(56, 302, 30, 21);

		txtDaysDeal = new Text(shell, SWT.BORDER);
		txtDaysDeal.setText("1");
		txtDaysDeal.setBounds(92, 300, 30, 21);

		Label label_3 = new Label(shell, SWT.NONE);
		label_3.setText("\u5929\u91CF\u589E\u8D85\u8FC7");
		label_3.setBounds(124, 304, 61, 16);

		txtDealBat = new Text(shell, SWT.BORDER);
		txtDealBat.setText("2");
		txtDealBat.setBounds(184, 300, 30, 21);

		final Button btnWaveLarger = new Button(shell, SWT.CHECK);
		btnWaveLarger.setText("\u8FD120\u5929\u6CE2\u52A8\u503C\u5927\u4E8E");
		btnWaveLarger.setBounds(294, 261, 116, 21);

		txtWaveLarger = new Text(shell, SWT.BORDER);
		txtWaveLarger.setText("60");
		txtWaveLarger.setBounds(408, 261, 35, 21);

		final Button btnIdeal = new Button(shell, SWT.CHECK);
		btnIdeal.setText("\u76EE\u6807\u6DA8\u5E45\u8D85\u8FC7");
		btnIdeal.setBounds(294, 317, 93, 21);

		txtIdeal = new Text(shell, SWT.BORDER);
		txtIdeal.setBounds(393, 315, 30, 21);

		final Button btnMaxToDays = new Button(shell, SWT.CHECK);
		btnMaxToDays.setText("\u8FD1");
		btnMaxToDays.setBounds(294, 346, 45, 21);

		txtMaxToDays = new Text(shell, SWT.BORDER);
		txtMaxToDays.setText("1");
		txtMaxToDays.setBounds(338, 344, 30, 21);

		Label label_4 = new Label(shell, SWT.NONE);
		label_4.setText("\u5929\u51FA\u73B0\u6700\u9AD8\u4EF7");
		label_4.setBounds(374, 349, 93, 16);

		final Button btnMinToDays = new Button(shell, SWT.CHECK);
		btnMinToDays.setText("\u8FD1");
		btnMinToDays.setBounds(294, 375, 45, 21);

		txtMinToDays = new Text(shell, SWT.BORDER);
		txtMinToDays.setText("2");
		txtMinToDays.setBounds(338, 373, 30, 21);

		Label label_5 = new Label(shell, SWT.NONE);
		label_5.setText("\u5929\u51FA\u73B0\u6700\u4F4E\u4EF7");
		label_5.setBounds(374, 378, 93, 16);

		final Button btnWaveLower = new Button(shell, SWT.CHECK);
		btnWaveLower.setText("\u8FD120\u5929\u6CE2\u52A8\u503C\u5C0F\u4E8E");
		btnWaveLower.setBounds(294, 288, 116, 21);

		txtWaveLower = new Text(shell, SWT.BORDER);
		txtWaveLower.setText("200");
		txtWaveLower.setBounds(408, 288, 35, 21);

		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Connection con = ConnectionFactory.getConnection();

				if (btnHighLevel.getSelection()) {
					where.append(" and priceLevel =1 ");
				}

				if (btnLowLevel.getSelection()) {
					where.append(" and priceLevel =-1 ");
				}

				if (btnDeal.getSelection()) {
					where.append(" and amtCreDays <= " + txtDaysDeal.getText()
							+ " and amountBat>" + txtDealBat.getText());
				}

				if (BtnLowDays.getSelection()) {
					where.append(" and fallDays >= " + txtDwonDays.getText());
				}

				if (BtnUpDays.getSelection()) {
					where.append(" and upDays >= " + txtUpDays.getText());
				}

				if (btnScoreHigher.getSelection()) {
					where.append(" and score >=" + txtScoreHigher.getText());
				}

				if (btnScoreLower.getSelection()) {
					where.append(" and score <=" + txtScoreLower.getText());
				}

				if (BtnIndustry.getSelection()) {
					where.append(" and (industry like '%"
							+ txtIndustry.getText()
							+ "%' or sinaIndustry like '%"
							+ txtIndustry.getText() + "%' or area like '%"
							+ txtIndustry.getText() + "%')");
				}

				if (btnWaveLarger.getSelection()) {
					where.append(" and dayPercentWave>="
							+ txtWaveLarger.getText());
				}

				if (btnWaveLower.getSelection()) {
					where.append(" and dayPercentWave<="
							+ txtWaveLower.getText());
				}

				if (btnIdeal.getSelection()) {
					where.append(" and idealPrice>=curPrice*"
							+ txtIdeal.getText() + "/100");
				}

				if (btnMaxToDays.getSelection()) {
					where.append(" and maxToDays <=" + txtMaxToDays.getText());
				}

				if (btnMinToDays.getSelection()) {
					where.append(" and minToDays <=" + txtMinToDays.getText());
				}

				String temptable = "";
				//
				if (btnDaysdown.getSelection()) {
					String dealdaysSql = "select dealday from stockdaydeal where number = 'sh000001' order by dealday desc limit 0,"
							+ txtDaysDown.getText();
					Statement stmt = null;
					ResultSet rs = null;
					try {
						stmt = con.createStatement();
						rs = stmt.executeQuery(dealdaysSql);
						Date minDate = null;
						while (rs.next()) {
							minDate = rs.getDate(1);
						}
						temptable = " join (select number from stockdaydeal where dealday >='"
								+ minDate
								+ "' and qty<>0 group by number having sum(ratepercent)<="
								+ txtDownPer.getText()
								+ ") as t on t.number=stockinfo.number ";
					} catch (SQLException e1) {
						e1.printStackTrace();
					} finally {
						try {
							if (rs != null) {
								rs.close();
							}
							if (stmt != null) {
								stmt.close();
							}
						} catch (SQLException ex) {
							ex.printStackTrace();
						}
					}
				}

				if (btnDaysUp.getSelection()) {
					String dealdaysSql = "select dealday from stockdaydeal where number = 'sh000001' order by dealday desc limit 0,"
							+ txtDaysUp.getText();
					Statement stmt = null;
					ResultSet rs = null;
					try {
						stmt = con.createStatement();
						rs = stmt.executeQuery(dealdaysSql);
						Date minDate = null;
						while (rs.next()) {
							minDate = rs.getDate(1);
						}
						temptable = " join (select number from stockdaydeal where dealday >='"
								+ minDate
								+ "' and qty<>0 group by number having sum(ratepercent)>="
								+ txtUpPer.getText()
								+ ") as t on t.number=stockinfo.number ";
					} catch (SQLException e1) {
						e1.printStackTrace();
					} finally {
						try {
							if (rs != null) {
								rs.close();
							}
							if (stmt != null) {
								stmt.close();
							}
						} catch (SQLException ex) {
							ex.printStackTrace();
						}
					}
				}

				Statement stmt = null;
				ResultSet rs = null;

				try {
					stmt = con.createStatement();
					System.out
							.println("select stockinfo.number,stockanlyz.maxPice,stockanlyz.averAmount FROM stockinfo join stockanlyz on stockinfo.number=stockanlyz.number "
									+ temptable + where.toString());
					rs = stmt
							.executeQuery("select stockinfo.number,stockanlyz.maxPice,stockanlyz.averAmount FROM stockinfo join stockanlyz on stockinfo.number=stockanlyz.number "
									+ temptable + where.toString());
					while (rs.next()) {
						// boolean isSelected = true;
						TimeStock ts = LoadData.getTimeStock(rs.getString(1));
						if (ts == null || ts.getQty() == 0) {
							continue;
						}

						if (btnCurOverMaxPrice.getSelection()) {
							if (ts.getCurPrice().compareTo(rs.getBigDecimal(2)) < 0) {
								continue;
							}
						}

						if (btnCurMaxPrice.getSelection()) {
							if (ts.getMaxPrice()
									.subtract(ts.getCurPrice())
									.divide(ts.getPreDayPrice(), 4,
											BigDecimal.ROUND_HALF_UP)
									.compareTo(new BigDecimal(0.01)) >= 0) {
								continue;
							}
						}

						if (btnCurBigDeal.getSelection()) {
							if (ts.getAmount()
									.divide(rs.getBigDecimal(3).multiply(
											TimeUtil.proRate), 2,
											BigDecimal.ROUND_HALF_UP)
									.compareTo(new BigDecimal(1.8)) < 0) {
								continue;
							}
						}
						
						if (btnCurPreBat.getSelection()) {
							if(ts.getLastStock().getAmount().compareTo(Constant.ZERO)==0){
								continue;
							}
							if (ts.getAmount()
									.divide(ts.getLastStock().getAmount().multiply(
											TimeUtil.proRate), 2,
											BigDecimal.ROUND_HALF_UP)
									.compareTo(new BigDecimal(1.8)) < 0) {
								continue;
							}
						}

						if (btnCurPerUp.getSelection()) {
							if (ts.getRatePercent().compareTo(
									new BigDecimal(txtCurPerUp.getText())) < 0) {
								continue;
							}
						}

						if (btnCurPerDown.getSelection()) {
							if (ts.getRatePercent().compareTo(
									new BigDecimal(txtCurPerDown.getText())) > 0) {
								continue;
							}
						}

						if (btnCurUpToLow.getSelection()) {
							if (ts.getCurPrice()
									.subtract(ts.getMinPrice())
									.multiply(Constant.ONEHUNDRED)
									.divide(ts.getMinPrice(), 2,
											BigDecimal.ROUND_HALF_UP)
									.compareTo(
											new BigDecimal(txtLowBack.getText())) < 0) {
								continue;
							}
						}

						codeSet.add(ts.getCode());
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				} finally {
					try {
						if (rs != null) {
							rs.close();
						}
						if (stmt != null) {
							stmt.close();
						}
					} catch (SQLException ex) {
						ex.printStackTrace();
					}
				}
				shell.dispose();
			}
		});
		button.setBounds(747, 410, 69, 25);
		button.setText("\u786E\u5B9A");

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBounds(482, 10, 334, 384);

		table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setBounds(10, 10, 314, 364);

		TableColumn colSeq = new TableColumn(table, SWT.NONE);
		colSeq.setResizable(false);
		colSeq.setWidth(36);
		colSeq.setText("\u884C\u53F7");

		TableColumn colName = new TableColumn(table, SWT.NONE);
		colName.setWidth(98);
		colName.setText("\u540D\u79F0");
		
		TableColumn colValue = new TableColumn(table, SWT.NONE);
		colValue.setWidth(52);
		colValue.setText("\u503C");
		
		TableColumn colId = new TableColumn(table, SWT.NONE);
		colId.setWidth(34);
		colId.setText("id");
		
		TableColumn colMark = new TableColumn(table, SWT.NONE);
		colMark.setWidth(100);
		colMark.setText("\u5907\u6CE8");
		
		loadData();
		
		table.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.keyCode==SWT.DEL){
					int index = table.getSelectionIndex();
					if(index>=0){
						String id = table.getItem(index).getText(3);
						SqlUtil.update("delete from querysolution where id = "+id);
						loadData();
					}
				}
			}
		});
		
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				int index = table.getSelectionIndex();
				if(index>=0){
					btnHighLevel.setSelection(false);
					btnLowLevel.setSelection(false);
					btnDeal.setSelection(false);
					BtnLowDays.setSelection(false);
					BtnUpDays.setSelection(false);
					btnScoreHigher.setSelection(false);
					btnScoreLower.setSelection(false);
					BtnIndustry.setSelection(false);
					btnWaveLarger.setSelection(false);
					btnWaveLower.setSelection(false);
					btnIdeal.setSelection(false);
					btnMaxToDays.setSelection(false);
					btnMinToDays.setSelection(false);
					btnDaysdown.setSelection(false);
					btnDaysUp.setSelection(false);
					btnCurOverMaxPrice.setSelection(false);
					btnCurMaxPrice.setSelection(false);
					btnCurBigDeal.setSelection(false);
					btnCurPreBat.setSelection(false);
					btnCurPerUp.setSelection(false);
					btnCurPerDown.setSelection(false);
					btnCurUpToLow.setSelection(false);

					String value = table.getItem(index).getText(2);
					String[] keys = value.split(",");
					for (int i = 0; i < keys.length; i++) {
						if("btnHighLevel".equals(keys[i])){
							btnHighLevel.setSelection(true);
							continue;
						}
						
						if("btnLowLevel".equals(keys[i])){
							btnLowLevel.setSelection(true);
							continue;
						}
						
						if("btnDeal".equals(keys[i])){
							btnDeal.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							String[] str2 = keys[i+2].split("=");
							txtDaysDeal.setText(str1[1]);
							txtDealBat.setText(str2[1]);
							i=i+2;
							continue;
						}
						
						if("BtnLowDays".equals(keys[i])){
							BtnLowDays.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtDwonDays.setText(str1[1]);
							i++;
							continue;
						}
						
						if("BtnUpDays".equals(keys[i])){
							BtnUpDays.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtUpDays.setText(str1[1]);
							i++;
							continue;
						}

						if("btnScoreHigher".equals(keys[i])){
							btnScoreHigher.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtScoreHigher.setText(str1[1]);
							i++;
							continue;
						}

						if("btnScoreLower".equals(keys[i])){
							btnScoreLower.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtScoreLower.setText(str1[1]);
							i++;
							continue;
						}
						
						if("BtnIndustry".equals(keys[i])){
							BtnIndustry.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtIndustry.setText(str1[1]);
							i++;
							continue;
						}
						
						if("btnWaveLarger".equals(keys[i])){
							btnWaveLarger.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtWaveLarger.setText(str1[1]);
							i++;
							continue;
						}

						if("btnWaveLower".equals(keys[i])){
							btnWaveLower.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtWaveLower.setText(str1[1]);
							i++;
							continue;
						}

						if("btnIdeal".equals(keys[i])){
							btnIdeal.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtIdeal.setText(str1[1]);
							i++;
							continue;
						}
						
						if("btnMaxToDays".equals(keys[i])){
							btnMaxToDays.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtMaxToDays.setText(str1[1]);
							i++;
							continue;
						}
						
						if("btnMinToDays".equals(keys[i])){
							btnMinToDays.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtMinToDays.setText(str1[1]);
							i++;
							continue;
						}

						if("btnDaysdown".equals(keys[i])){
							btnDaysdown.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtDaysDown.setText(str1[1]);
							String[] str2 = keys[i+2].split("=");
							txtDownPer.setText(str2[1]);
							i=i+2;
							continue;
						}
						
						if("btnDaysUp".equals(keys[i])){
							btnDaysUp.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtDaysUp.setText(str1[1]);
							String[] str2 = keys[i+2].split("=");
							txtUpPer.setText(str2[1]);
							i=i+2;
							continue;
						}

						if("btnCurOverMaxPrice".equals(keys[i])){
							btnCurOverMaxPrice.setSelection(true);
							continue;
						}
						
						if("btnCurMaxPrice".equals(keys[i])){
							btnCurMaxPrice.setSelection(true);
							continue;
						}
						
						if("btnCurBigDeal".equals(keys[i])){
							btnCurBigDeal.setSelection(true);
							continue;
						}
						
						if("btnCurPreBat".equals(keys[i])){
							btnCurPreBat.setSelection(true);
							continue;
						}
						
						if("btnCurPerUp".equals(keys[i])){
							btnCurPerUp.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtCurPerUp.setText(str1[1]);
							i++;
							continue;
						}

						if("btnCurPerDown".equals(keys[i])){
							btnCurPerDown.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtCurPerDown.setText(str1[1]);
							i++;
							continue;
						}

						if("btnCurUpToLow".equals(keys[i])){
							btnCurUpToLow.setSelection(true);
							String[] str1 = keys[i+1].split("=");
							txtLowBack.setText(str1[1]);
							i++;
							continue;
						}
					}
				}
			}
		});

		Button btnSave = new Button(shell, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				StringBuffer queryStr = new StringBuffer();
				QueryNameUi qni = new QueryNameUi(shell, SWT.NONE);
				QuerySolutionInfo queryName = qni.open();

				if (btnHighLevel.getSelection()) {
					queryStr.append("btnHighLevel,");
				}

				if (btnLowLevel.getSelection()) {
					queryStr.append("btnLowLevel,");
				}

				if (btnDeal.getSelection()) {
					queryStr.append("btnDeal,").append("txtDaysDeal=" + txtDaysDeal.getText()).append(",txtDealBat="+txtDealBat.getText())
					.append(",");
				}

				if (BtnLowDays.getSelection()) {
					queryStr.append("BtnLowDays,").append("txtDwonDays=" + txtDwonDays.getText()).append(",");
				}

				if (BtnUpDays.getSelection()) {
					queryStr.append("BtnUpDays,").append("txtUpDays=" + txtUpDays.getText()).append(",");
				}

				if (btnScoreHigher.getSelection()) {
					queryStr.append("btnScoreHigher,").append("txtScoreHigher=" + txtScoreHigher.getText()).append(",");
				}

				if (btnScoreLower.getSelection()) {
					queryStr.append("btnScoreLower,").append("txtScoreLower=" + txtScoreLower.getText()).append(",");
				}

				if (BtnIndustry.getSelection()) {
					queryStr.append("BtnIndustry,").append("txtIndustry=" + txtIndustry.getText()).append(",");
				}

				if (btnWaveLarger.getSelection()) {
					queryStr.append("btnWaveLarger,").append("txtWaveLarger="+ txtWaveLarger.getText()).append(",");
				}

				if (btnWaveLower.getSelection()) {
					queryStr.append("btnWaveLower,").append("txtWaveLower=" + txtWaveLower.getText()).append(",");
				}

				if (btnIdeal.getSelection()) {
					queryStr.append("btnIdeal,").append("txtIdeal=" + txtIdeal.getText()).append(",");
				}

				if (btnMaxToDays.getSelection()) {
					queryStr.append("btnMaxToDays,").append("txtMaxToDays=" + txtMaxToDays.getText()).append(",");
				}

				if (btnMinToDays.getSelection()) {
					queryStr.append("btnMinToDays,").append("txtMinToDays=" + txtMinToDays.getText()).append(",");
				}

				if (btnDaysdown.getSelection()) {
					queryStr.append("btnDaysdown,").append("txtDaysDown=" + txtDaysDown.getText()).append(",")
					.append("txtDownPer=" + txtDownPer.getText()).append(",");
				}

				if (btnDaysUp.getSelection()) {
					queryStr.append("btnDaysUp,").append("txtDaysUp=" + txtDaysUp.getText()).append(",")
					.append("txtUpPer=" + txtUpPer.getText()).append(",");
				}
				if (btnCurOverMaxPrice.getSelection()) {
					queryStr.append("btnCurOverMaxPrice,");
				}

				if (btnCurMaxPrice.getSelection()) {
					queryStr.append("btnCurMaxPrice,");
				}

				if (btnCurBigDeal.getSelection()) {
					queryStr.append("btnCurBigDeal,");
				}
				
				if (btnCurPreBat.getSelection()) {
					queryStr.append("btnCurPreBat,");
				}

				if (btnCurPerUp.getSelection()) {
					queryStr.append("btnCurPerUp,").append("txtCurPerUp="+txtCurPerUp.getText()).append(",");
				}

				if (btnCurPerDown.getSelection()) {
					queryStr.append("btnCurPerDown,").append("txtCurPerDown="+txtCurPerDown.getText()).append(",");
				}

				if (btnCurUpToLow.getSelection()) {
					queryStr.append("btnCurUpToLow,").append("txtLowBack="+txtLowBack.getText()).append(",");
				}
				
				String sql = "insert into querysolution(name,value,mark) values('"+queryName.getName()+"','"+queryStr.substring(0, queryStr.length()-1)
						+"','"+queryName.getMark()+"')";
				SqlUtil.update(sql);
				
				loadData();
			}
		});

		btnSave.setBounds(387, 410, 69, 25);
		btnSave.setText("\u4FDD\u5B58\u65B9\u6848");
		
	}
	
	private void loadData(){
		Connection con = ConnectionFactory.getConnection();
		Statement stmt = null;
		ResultSet rs = null;
		
		try {
			stmt=con.createStatement();
			rs = stmt.executeQuery("select * from querysolution");
			int i = 0;
			table.removeAll();
			while (rs.next()) {
				TableItem ti = new TableItem(table, SWT.NONE);
				String name = rs.getString("name");
				String value = rs.getString("value");
				String id = rs.getString("id");
				String mark = rs.getString("mark");
				ti.setText(new String[]{String.valueOf(++i),name,value,id,mark});
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
				}
				if(stmt!=null){
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
